package com.troila.cloud.simplify.web.controller.rest;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.troila.cloud.simplify.model.QuestionModel;
import com.troila.cloud.simplify.model.SAnswer;
import com.troila.cloud.simplify.model.SOption;
import com.troila.cloud.simplify.model.SQuestion;
import com.troila.cloud.simplify.model.STitle;
import com.troila.cloud.simplify.service.SAnswerService;
import com.troila.cloud.simplify.service.SOptionService;
import com.troila.cloud.simplify.service.SQuestionService;
import com.troila.cloud.simplify.service.STitleService;

@RestController
@RequestMapping("/api")
public class ApiController {
	@Autowired
	private SAnswerService sAnswerService;
	@Autowired
	private SOptionService sOptionService;
	@Autowired
	private SQuestionService sQuestionService;
	@Autowired
	private STitleService sTitleService;
	
	/**
	 * 
	 * 选择答案数量+1. <br/>
	 *
	 * @author wangxuewei
	 */
	@GetMapping("/answerNum")
	@ResponseBody
	public void answerNum(HttpSession session,@RequestParam int id){
		sAnswerService.answerNum(id);
	}
	/**
	 * 
	 * 删除问题. <br/>
	 *
	 * @author wangxuewei
	 */
	@GetMapping("/delQuestion")
	@ResponseBody
	public void delQuestion(HttpSession session,@RequestParam int id){
		sQuestionService.delQuestion(id);
	}
	/**
	 * 
	 * 删除选项. <br/>
	 *
	 * @author wangxuewei
	 */
	@GetMapping("/delOption")
	@ResponseBody
	public void delOption(HttpSession session,@RequestParam int id){
		sOptionService.delOption(id);
	}
	/**
	 * 
	 * 删除答案. <br/>
	 *
	 * @author wangxuewei
	 */
	@GetMapping("/delAnswer")
	@ResponseBody
	public void delAnswer(HttpSession session,@RequestParam int id){
		sAnswerService.delAnswer(id);
	}
	/**
	 * 
	 * saveAll:保存title. <br/>
	 *
	 * @author wangxuewei
	 */
	@PostMapping("/saveTitle")
	@ResponseBody
	public STitle saveTitle(HttpSession session,@RequestBody STitle sTitle){
		
		return sTitleService.save(sTitle);
	}
	
	/**
	 * 
	 * saveAll:保存title. <br/>
	 *
	 * @author wangxuewei
	 */
	@GetMapping("/getTitleList")
	@ResponseBody
	public Page<STitle> getTitleList(HttpSession session,
			@RequestParam(required = true, value = "mediaKey") String mediaKey,
			@RequestParam(required = false, defaultValue = "0", value = "page") int page,
			@RequestParam(required = false, defaultValue = "10", value = "size") int size){
		
		return sTitleService.getTitleList(page,size,mediaKey);
	}
	/**
	 * 
	 * saveAll:保存title. <br/>
	 *
	 * @author wangxuewei
	 */
	@GetMapping("/getTitleListByStatus")
	@ResponseBody
	public Page<STitle> getTitleListByStatus(HttpSession session,
			@RequestParam(required = true, value = "mediaKey") String mediaKey,
			@RequestParam(required = false, defaultValue = "0", value = "page") int page,
			@RequestParam(required = false, defaultValue = "10", value = "size") int size){
		
		return sTitleService.getTitleListByStatus(page,size,mediaKey);
	}
	
	/**
	 * 
	 * saveAll:保存问题和答案集合. <br/>
	 *
	 * @author wangxuewei
	 */
	@PostMapping("/saveAll")
	@ResponseBody
	public QuestionModel saveAll(HttpSession session,@RequestBody QuestionModel questionModel){
		
		if(StringUtils.isEmpty(questionModel) || StringUtils.isEmpty(questionModel.getAnswers()) || StringUtils.isEmpty(questionModel.getOptions()) || StringUtils.isEmpty(questionModel.getQuestions())) {
			return null;
		}
		
		sAnswerService.save(questionModel.getAnswers());
		sOptionService.save(questionModel.getOptions());
		sQuestionService.save(questionModel.getQuestions());
		
		return questionModel;
	}

	
	@GetMapping("/getAllQuestion")
	@ResponseBody
	public QuestionModel getAllQuestion(HttpSession session,int titleId){
		List<SAnswer> answers = sAnswerService.findByATitleId(titleId);
		List<SOption> options = sOptionService.findByATitleId(titleId);
		List<SQuestion> questions = sQuestionService.findByATitleId(titleId);
		QuestionModel qm = new QuestionModel();
		qm.setAnswers(answers);
		qm.setOptions(options);
		qm.setQuestions(questions);
		
		return qm;
	}
	
	
}